package com.taoyi.mhouse.behavior;

import android.animation.Animator;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewPropertyAnimator;
import android.view.animation.Interpolator;

/**
 * Created by tempo on 2015/11/18.
 */
public class FooterBehavior extends CoordinatorLayout.Behavior<View> {
    private static final Interpolator INTERPOLATOR = new FastOutSlowInInterpolator();
    private static String TAG = FooterBehavior.class.getSimpleName();
    boolean isIdle=true;
    boolean isHinding=false;
    private int sinceDirectionChange;


    public FooterBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    //1.判断滑动的方向 我们需要垂直滑动
    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {
        return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
    }

    //2.根据滑动的距离显示和隐藏footer view

    @Override
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, View child, View target, int dx, int dy, int[] consumed) {
        if (dy > 0 && sinceDirectionChange < 0 || dy < 0 && sinceDirectionChange > 0) {
            sinceDirectionChange = 0;
        }
        sinceDirectionChange += dy;
        if (sinceDirectionChange > child.getHeight()&&child.getVisibility() == View.VISIBLE && isIdle&&!isHinding) {
            isIdle=false;
            hide(child);
        } else if (sinceDirectionChange < 0&&isIdle&&isHinding) {
            isIdle=false;
            show(child);
        }
    }


    private void hide(final View view) {
        Log.i(TAG, "hide");
        view.setVisibility(View.VISIBLE);
        ViewPropertyAnimator animator = view.animate().translationY(view.getHeight()).setDuration(200);//setInterpolator(INTERPOLATOR);
        animator.setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                isIdle=true;
                isHinding=true;
            }

            @Override
            public void onAnimationCancel(Animator animator) {

                hide(view);
//                isIdle=true;
            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        animator.start();
    }


    private void show(final View view) {
        Log.i(TAG, "show");
        ViewPropertyAnimator animator = view.animate().translationY(0).setDuration(200);//.setInterpolator(INTERPOLATOR);
        animator.setListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
//                view.setVisibility(View.GONE);
               isIdle=true;
                isHinding=false;
            }

            @Override
            public void onAnimationCancel(Animator animator) {
//                show(view);
                isIdle = true;
            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        animator.start();

    }
}
